package FileManager;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import DiskManager.Disk;
import DiskManager.DiskBit;
import FileMangerClient.MyComputerController;
import Tools.AddSomething;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.FlowPane;

public class SimulationDirectory extends SimulateContent {
	AddSomething addSomething = new AddSomething();
    String content="";
    char[]   ContentArrary;
    
    private  Set<SimulateContent> children = new HashSet<SimulateContent>();
    private  Map<SimulateContent,TreeItem<String>>  childrentreeView = new   HashMap<SimulateContent,TreeItem<String>>();    
    
	public Set<SimulateContent> getChildren() {
	return children;
    }

public Map<SimulateContent, TreeItem<String>> getChildrentreeView() {
		return childrentreeView;
	}

	public void setChildrentreeView(Map<SimulateContent, TreeItem<String>> childrentreeView) {
		this.childrentreeView = childrentreeView;
	}

public void setChildren(Set<SimulateContent> children) {
	this.children = children;
}
    
public SimulationDirectory(String name,String ExtendName,String attribute,int stranum,SimulationDirectory father,Disk disk) {
	setName(name);
	setExtendName(ExtendName);
	setattribute(attribute);
	setstranum(stranum);
	setSize(16);
	this.father = father;
	if(this.father!=null) 
	this.father.addChildren(this);
              	}
public SimulationDirectory(String name,String ExtendName,String attribute) {
	setName(name);
	setExtendName(ExtendName);
	setattribute(attribute);

	setSize(16);
              	}
	    public void addChildren(SimulateContent child,TreeItem<String> childTreeItem) {
	    	
	    	
			 children.add(child); 
			 childrentreeView.put(child, childTreeItem);
		}
	    public void addChildren(SimulateContent child) {
			 children.add(child); 
			 //保证子文件的Startnum是以3位的形式存进文件夹
			 String string="";
			 if(child.getStartnum()<10)
			      string = "00"+String.valueOf(child.getStartnum());	 
			 else if(child.getStartnum()<100)
				  string = "0"+String.valueOf(child.getStartnum());	
			 else 
				 string = String.valueOf(child.getStartnum());	
	         if(this.content.equals(""))
	        	 this.content=string;
	         else
			 this.content+=string;
			 this.ContentArrary = this.content.toCharArray();
			 this.changeSize(this, child.getSize());  //增加内存
		}
	    public boolean deleteChild(SimulateContent child,Disk disk) {
	       char[] c = new char[3]; 
	    	if(children.contains(child))
	    	{
	    		int j=0;
	        for(int i=0;i<ContentArrary.length/3;i++)//将文件夹中保存的子文件Startnum置为0
	        {
	            c[0]=ContentArrary[j++]; 
	            c[1]=ContentArrary[j++];
	            c[2]=ContentArrary[j++];
	            int Startnum = Integer.valueOf(new String(c));
	            if(child.getStartnum()==Startnum)
	            {
	            	ContentArrary[j-3]='0';
	            	ContentArrary[j-2]='0';
	            	ContentArrary[j-1]='0';
	            }
	        }
	        this.content = new String(this.ContentArrary);
	        this.changeSize(this,-child.getSize());
	        child.clear(disk);
	        this.saveContentArrary(disk);
	    	children.remove(child);
	    	childrentreeView.remove(child);
	    	return true;
			}
	    	return false;  	
	    }
	   
	    public void Search(String name,MyComputerController myComputerController) {
	    	SimulationDirectory simulationDirectory1;
	    	for(SimulateContent child :children)
	    	{
	    		if(child.getName().equals(name))
	    		{
	    			System.out.println(child.getName()+" "+name+" "+child.getName().equals(name));
	    			if(child.getAttribute().equals("D"))
	    			{
	    				addSomething.AddSearchResult("/FileMangerClient/ddd.png", (SimulationDirectory)child, myComputerController);
	    			}
	    			else 
	    			{
	    				addSomething.AddSearchResult("/FileMangerClient/File.png", (SimulationFile)child, myComputerController);
	    			}
	    		}
	    	    if(child.getAttribute().equals("D"))
	    	    {
	    	    	simulationDirectory1=(SimulationDirectory)child;
	    	    	simulationDirectory1.Search(name, myComputerController);
	    	    }
	    	}
	    }
	    
	    public void saveContentArrary(Disk disk) {
	    	int Num=0;
	    	if(this.getStartnum()==2) {
	    		switch(this.getName()) {
	    		case("C"):Num=3;break;
	    		case("D"):Num=63;break;
	    		case("E"):Num=123;break;
	    		case("F"):Num=183;break;
	    		}
	    		disk.save(Num, this.ContentArrary);
	    		return;
	    	}
	    	else Num=this.getStartnum();
	    	String sname = String.format("%6s",new String(this.name)); 
        	String sextendname = String.format("%2s",new String(this.ExtendName));
        	String sattribute = String.format("%1s",new String(this.attribute));
        	String sstartnum = String.format("%3s",new String(this.startnum));
        	String ssize = String.format("%4s",new String(this.size));
        	String all = new String();
        	all=sname+sextendname+sattribute+sstartnum+ssize;
        	all+=this.content;
        	char[] allContent = all.toCharArray();
        	System.out.println(sstartnum);
        	System.out.println(Arrays.toString(allContent));
        	disk.save(Num,allContent);
	    }
	    
	    public void initialize(Disk disk) {
	    	int Num=0;
	    	if(this.getStartnum()==2) {
	    		switch(this.getName()) {
	    		case("C"):Num=3;break;
	    		case("D"):Num=63;break;
	    		case("E"):Num=123;break;
	    		case("F"):Num=183;break;
	    		}	
		    	DiskBit diskbit = disk.getDiskBit()[Num];
				char[] openContent = diskbit.load();
				String gd = new String(openContent).replaceAll(" ", "");
				this.setContent(gd);
				return;
	    	}
	    	Num = this.getStartnum();
	    	DiskBit diskbit = disk.getDiskBit()[Num];
			char[] openContent = diskbit.load();
			String gd = new String(openContent);
			gd = gd.substring(0,15);
			this.setContent(gd);
	    }
	    public boolean isHaveName(String string) {
	    	for(SimulateContent s:this.children)
	    	{
	    		if(s.getName().equals(string))
	    			return true;
	    	}
	    	return false;
	    }
	    //
	    public String getContent() {
			return content;
		}
		public char[] getContentArrary() {
			return ContentArrary;
		}
		public void setContent(String content) {
			this.content = content;
			ContentArrary = content.toCharArray();
		}
}
